package com.ltv.xiangqiplugin.controllers;

import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.ltv.xiangqiplugin.models.MoveStack;
import com.ltv.xiangqiplugin.models.PositionStack;

public class MoveListControl implements ListSelectionListener {

	private static MoveListControl instance;
	
	private MoveListControl() {
		
	}
	
	@Override
	public void valueChanged(ListSelectionEvent event) {
		// Fix bug
		BoardViewControl.defaultControl().setOriginPanel(null);
				
		JList theList = (JList) event.getSource();
		if (theList == null) {
			return;
		}
		
		if (event.getValueIsAdjusting() == false) {
			int index = theList.getSelectedIndex();
			PositionStack.getInstance().setIndex(MoveStack.getInstance().getMoves().size() - 1 - index);
			PositionStack.getInstance().notifyObservers();
			
			MoveStack.getInstance().setIndex(MoveStack.getInstance().getMoves().size() - 1 - index);
		}
		
	}

	public static MoveListControl getInstance() {
		if (instance == null) {
			instance = new MoveListControl();
		}
		
		return instance;
	}
	
}
